﻿using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.Log;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Catastrophe.BasePC.UI.SystemIni.BaseData
{
    /// <summary>
    /// 数据库基本数据
    /// </summary>
    public class DataIniEntity
    {

        /// <summary>
        /// 菜单
        /// </summary>
        public List<SysMenuEntity> dataSysMenuEntity;
        /// <summary>
        /// 系统日志
        /// </summary>
        public List<SysLogEntity> dataSysLogEntity;
        /// <summary>
        /// 登录日志
        /// </summary>
        public List<SysOprRecordsEntity> dataSysOprRecordsEntity;
        /// <summary>
        /// 用户
        /// </summary>
        public List<SysUserEntity> dataSysUserEntity;
        /// <summary>
        /// 用户绑定的菜单
        /// </summary>
        public List<SysRoleMenuEntity> dataSysRoleMenuEntity;
        /// <summary>
        /// 词典
        /// </summary>
        public List<SysDictionaryEntity> dataSysDictionary;
        /// <summary>
        /// 命名空间
        /// </summary>
        string assembly = "Catastrophe.BasePC.UI";
        /// <summary>
        /// 窗口类型
        /// </summary>
        string formType = "UserControl";
        /// <summary>
        /// 生命周期
        /// </summary>
        string InstanceLife = "PerDependency";
        /// <summary>
        /// 加载模式
        /// </summary>
        string LoadMode = "IOC";
        


        /// <summary>
        /// 菜单管理
        /// </summary>
        public const string name_Menu = "Menu";
        /// <summary>
        /// 实例窗体
        /// </summary>
        public const string name_Hello = "Hello";
        /// <summary>
        /// 系统配置
        /// </summary>
        public const string name_FileConfig = "FileConfig";
        /// <summary>
        /// 系统日志
        /// </summary>
        public const string name_SysLog = "Log";
        /// <summary>
        /// 系统记录
        /// </summary>
        public const string name_SysOprRecords = "Records";
        /// <summary>
        /// 用户管理
        /// </summary>
        public const string name_SysUser = "User";
        /// <summary>
        /// 角色
        /// </summary>
        public const string name_SysRole = "SysRole";
        /// <summary>
        /// 部门
        /// </summary>
        public const string name_SysDepartment = "SysDepartment";
        /// <summary>
        /// 主窗口
        /// </summary>
        public const string name_Main = "MainWindow";
        /// <summary>
        /// emgucv
        /// </summary>
        public const string name_ucEmgucv = "ucEmgucv";
        /// <summary>
        /// 部门
        /// </summary>
        public const string dic_Type_部门 = "部门";
        /// <summary>
        /// 身份
        /// </summary>
        public const string dic_Type_身份 = "身份";
        /// <summary>
        /// 管理员
        /// </summary>
        public const string dic_Type_身份_管理员 = "管理员";

        

        public DataIniEntity()
        {
            dataSysMenuEntity = new List<SysMenuEntity>();
            dataSysLogEntity = new List<SysLogEntity>();
            dataSysOprRecordsEntity = new List<SysOprRecordsEntity>();
            dataSysRoleMenuEntity = new List<SysRoleMenuEntity>();
            dataSysUserEntity = new List<SysUserEntity>();
            dataSysDictionary = new ();
        }

        public void Ini()
        {
            IniSysMenuEntity();
            IniSysUserEntity();
            IniSysDictionary();
        }
        public void IniSysDictionary()
        {
            string type = dic_Type_部门;
            dataSysDictionary.Add(new SysDictionaryEntity()
            {
                DicName = "开发部",
                DicKey= type,
                DicValue= "开发部",
                TimeStamp=DateTime.Now.ToString()

            });
            dataSysDictionary.Add(new SysDictionaryEntity()
            {
                DicName = "客服部",
                DicKey = type,
                DicValue = "客服部",
                TimeStamp = DateTime.Now.ToString()
            });

            type = dic_Type_身份;
            dataSysDictionary.Add(new SysDictionaryEntity()
            {
                DicName = dic_Type_身份_管理员,
                DicKey = type,
                DicValue = dic_Type_身份_管理员,
                TimeStamp = DateTime.Now.ToString()
            });
        }
        public void IniSysRoleMenuEntity(List<SysMenuEntity> menus)
        {
            int id = 1;
            foreach (var user in dataSysUserEntity)
            {
                foreach (var menu in menus)
                {
                    SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity()
                    {
                        AssemblyName = "",
                        AuthorizeType = "",
                        Id = id++,
                        Identity = user.Identify,
                        MenuId = menu.Id,
                        NodeName = menu.NodeName,
                        NodeState = true,
                        UserName = user.UserName,
                        UserNo = user.UserNo,
                    };
                    dataSysRoleMenuEntity.Add(sysRoleMenuEntity);
                }
            }
        }

        private void IniSysUserEntity()
        {
            SysUserEntity user = new SysUserEntity()
            {
                UserName = "admin",
                Id = 1,
                LoginPassword = "2357",
                Identify = "管理员",
                TimeStamp = DateTime.Now.ToString(),
            };
            dataSysUserEntity.Add(user);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="index"></param>
        private void IniSysMenuEntity()
        {
            int index = 0;
            var frist = index;
            AddSysMenuEntityItem(index++, -1, name_Hello, "HelloWord");//+0
            AddSysMenuEntityItem(index++, -1, "", "系统管理");//+1
            AddSysMenuEntityItem(index++, -1, "", "系统记录");//+2
            AddSysMenuEntityItem(index++, -1, "", "用户管理");//+3
            AddSysMenuEntityItem(index++, -1, "", "代码生成器");//+4
            AddSysMenuEntityItem(index++, -1, "", "框架示例");//+5

            AddSysMenuEntityItem(index++, frist + 1, name_Menu, "菜单管理");
            AddSysMenuEntityItem(index++, frist + 1, name_FileConfig, "系统配置");

            AddSysMenuEntityItem(index++, frist + 2, name_SysOprRecords, "系统记录");
            AddSysMenuEntityItem(index++, frist + 2, name_SysLog, "系统日志");

            AddSysMenuEntityItem(index++, frist + 3, name_SysUser, "用户管理");
            AddSysMenuEntityItem(index++, frist + 3, name_SysDepartment, "部门管理");
            AddSysMenuEntityItem(index++, frist + 3, name_SysRole, "角色管理");

            AddSysMenuEntityItem(index++, frist + 5, name_ucEmgucv, "视觉Emgucv", "Catastrophe.Emgucv");
            AddSysMenuEntityItem(index++, frist + 5, "ucEmgucvCamera", "相机Emgucv", "Catastrophe.Emgucv");

        }

        private void AddSysMenuEntityItem(int BaseIndex, int ParentIndex, string ResourceName, string name,string assembly= "Catastrophe.BasePC.UI")
        {
            dataSysMenuEntity.Add(new SysMenuEntity()
            {
                Assembly = assembly,
                BaseIndex = BaseIndex++,
                ParentIndex = ParentIndex,
                FormType = formType,
                IsFixed = false,
                InstanceLife = InstanceLife,
                ResourceName = ResourceName,
                NodeName = name,
                LoadMode = LoadMode,
                SoftId = "[\"ALL\"]",
                TimeStamp = DateTime.Now.ToString(),
                Visible=true
            });
        }
    }
}
